//
//  UIButton+Timer.m
//  
//
//  Created by 张领瑞 on 2017/8/31.
//
//

#import "UIButton+Timer.h"


#import "PrefixHeader.pch"

@implementation UIButton (Timer)
-(void)leftSecondsShow{
    __weak typeof (self) weakself = self;
    __block int timeout = LEFTTIME60S;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0);//每秒执行
    dispatch_source_set_event_handler(timer, ^{
        if (timeout < 0) {
            dispatch_source_cancel(timer);//结束时，关闭
            dispatch_async(dispatch_get_main_queue(), ^{//设置显示
                //设置图片
                [weakself setTitle:@"" forState:UIControlStateNormal];
                [weakself setBackgroundImage:[UIImage imageNamed:@"send_authentication.png"] forState:UIControlStateNormal];
                weakself.userInteractionEnabled = YES;
            });
        }else{
            int seconds = timeout;
            NSString *strTime = [NSString stringWithFormat:@"%ds",seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                self.userInteractionEnabled = NO;
                
                //[UIView beginAnimations:nil context:nil];
                //[UIView setAnimationDuration:1];
                [weakself setBackgroundImage:[UIImage imageNamed:@"bth_bg.png"] forState:UIControlStateNormal];
                [weakself setTitle:strTime forState:UIControlStateNormal];
                //[UIView commitAnimations];
            });
            timeout--;
        }
    });
    dispatch_resume(timer);
}
@end
